 /**
  * Class that extends TerminalInput to differentiate terminal types
  * @class WireIt.util.WebUITerminalInput
  * @extends WireIt.util.TerminalInput
  * @constructor
  * @param {HTMLElement} parentEl Parent dom element
  * @param {Object} options configuration object
  * @param {WireIt.Container} container (Optional) Container containing this terminal
  * @author Igor Lazarev
  */
 
 WireIt.util.WebUITerminalInput = function(parentEl, options, container) {
   WireIt.util.WebUITerminalInput.superclass.constructor.call(this,parentEl, options, container);
	if((this.container.options.title == "Script")||(this.container.options.title == "JavaScript")||(this.container.options.title == "Python")){
		var type = "string";
		var name = options.name;
		YAHOO.util.Event.addListener(this.el, "click", function(){
			var pos = YAHOO.util.Dom.getXY(this.el);
			var X = pos[0];
			var Y = pos[1];
			var containerConfig = '{"xtype":"WireIt.WebUIInputContainer","title":"input","fields":[{"type": "myType", "inputParams": {"label": "Value", "name": "input", "wirable": false, "value": {"type": "' + type + '", "inputParams": {"name":"' + name + '","typeInvite": "input value"}}}}],"position":[' + (X - 290) + ',' + (Y - 180) + ']}';
			if (this.wires.length < 1) {
				var container = this.container.layer.addContainer(YAHOO.lang.JSON.parse(containerConfig));
				container.schema = {};
				var inputTerminal = container.addTerminal({
					xtype: "WireIt.util.WebUITerminalOutput",
					"name": options.name+":"+type,
					"ddConfig": {
						"type": "string"
					},
					"direction": [0, 1],
					"offsetPosition": {
						"left": 86,
						"bottom": -15
					}
				});
				container.setName(options.name);
				var wire = {
					"src": {
						"moduleId": WireIt.indexOf(container, this.container.layer.containers),
						"terminal": inputTerminal.options.name
					},
					"tgt": {
						"moduleId": WireIt.indexOf(this.container, this.container.layer.containers),
						"terminal": this.options.name
					},
					color:"rgb(240,191,186)"
				};
				this.container.layer.addWire(wire);
				this.redrawAllWires();
			}
			
		}, this, true);
	}
	else if ((this.container.options.title != "output")&&(this.container.options.title != "JavaScript")&&(this.container.options.title != "Python")) {
		var type = options.name.split(":")[1];
		var name = options.name.split(":")[0];
		var portJSON = this.container.resourceJSON.inputs[name];
//singleselect list
		if ((type == "string")&&(portJSON["enum"])){
			YAHOO.util.Event.addListener(this.el, "click", function(){
				var pos = YAHOO.util.Dom.getXY(this.el);
				var X = pos[0];
				var Y = pos[1];
				if (!portJSON.options) {
					var list = YAHOO.lang.JSON.stringify(portJSON["enum"]);
				}
				else {
					var list = new Array;
					var inverse = new Array;
					var numbered = portJSON["enum"];
					for (i in portJSON.options) {
						list[i] = '"'+portJSON.options[i].label+'"';
						inverse[portJSON.options[i].label]=i;
					}
					list = '['+list+']';
				}
				
				var containerConfig = '{"xtype":"WireIt.WebUIInputContainer","title":"input","fields":[{"type": "select", "inputParams": {"name": "list","className":"listField","selectValues": '+list+'}},{"type": "myType", "inputParams": {"name": "input", "wirable": false, "value": {"type": "' + type + '", "inputParams": {"name":"' + name + '","typeInvite": "input value"}}}}],"position":[' + (X - 290) + ',' + (Y - 180) + ']}';
				if (this.wires.length < 1) {
					var container = this.container.layer.addContainer(YAHOO.lang.JSON.parse(containerConfig));
					container.schema = portJSON;
					if (container.bodyEl.getElementsByClassName("listField")[0].getWidth() > container.el.getWidth()) {
						container.el.style.width = container.bodyEl.getElementsByClassName("listField")[0].getWidth() + 20 + 'px';
					}
					else {
						container.el.style.width=container.el.getWidth();
					}
									
						var value = container.getValue();
						if (!portJSON.options) {
							value.input.inputParams.value = value.list;
						}
						else {
							value.input.inputParams.value = numbered[inverse[value.list]];
						}
						container.setValue(value);
					
					container.bodyEl.getElementsByClassName("inputEx-StringField-wrapper")[0].style.display='none';
					
					YAHOO.util.Event.addListener(container.bodyEl.getElementsByClassName("listField")[0].parentNode.parentNode.parentNode,"change",function() {
						value = container.getValue();
						if (!portJSON.options) {
							value.input.inputParams.value = value.list;
						}
						else {
							value.input.inputParams.value = numbered[inverse[value.list]];
						}
						container.setValue(value);
						container.bodyEl.getElementsByClassName("inputEx-StringField-wrapper")[0].style.display='none';
					},this,true);
		
					var inputTerminal = container.addTerminal({
						xtype: "WireIt.util.WebUITerminalOutput",
						"name": options.name,
						"ddConfig": {
							"type": options.name.split(":")[1]
						},
						"direction": [0, 1],
						"offsetPosition": {
							"left": Math.floor((container.el.getWidth())/2)-15,
							"bottom": -15
						}
					});
					container.setName(options.name.split(":")[0]);
					if (options.name.split(":")[1] == "file") {
						container.renderUploadLink();
					}
					var wire = {
						"src": {
							"moduleId": WireIt.indexOf(container, this.container.layer.containers),
							"terminal": inputTerminal.options.name
						},
						"tgt": {
							"moduleId": WireIt.indexOf(this.container, this.container.layer.containers),
							"terminal": this.options.name
						}
					};
					this.container.layer.addWire(wire);
					this.redrawAllWires();
				}
				
			}, this, true);	
		}
//multiselect list
		else if ((type == "array")&&(portJSON.items["enum"])){
			YAHOO.util.Event.addListener(this.el, "click", function(){
			var pos = YAHOO.util.Dom.getXY(this.el);
			var X = pos[0];
			var Y = pos[1];
			var items = portJSON["items"];
			var listOptions = items["options"];
			
			var list = new Array;
			var inverse = new Array;
			var numbered = portJSON["enum"];
			var names = new Array;
			
			list = YAHOO.lang.JSON.stringify(items["enum"]);
			names[0] ='"select multiple.."';
			for (i=0;i<listOptions.length;i++) {
				names[i+1]='"'+listOptions[i].label+'"';
			}
			list = '["stub",'+list.split('[')[1];
			names = "["+names+"]"
			
			var containerConfig = '{"xtype":"WireIt.WebUIInputContainer","title":"input","fields":[{"type": "multiselect", "inputParams": {"name": "list", "className":"listField","selectValues":'+list+',"selectOptions":'+names+'}},{"type": "myType", "inputParams": {"name": "input", "wirable": false, "value": {"type": "' + type + '", "inputParams": {"name":"' + name + '","typeInvite": "input value"}}}}],"position":[' + (X - 290) + ',' + (Y - 180) + ']}';
			
				if (this.wires.length < 1) {
					var container = this.container.layer.addContainer(YAHOO.lang.JSON.parse(containerConfig));
					container.schema = portJSON;
					if (container.bodyEl.getElementsByClassName("listField")[0].getWidth() > container.el.getWidth()) {
						container.el.style.width = container.bodyEl.getElementsByClassName("listField")[0].getWidth() + 20 + 'px';
					}
					
					container.bodyEl.getElementsByClassName("inputEx-StringField-wrapper")[0].style.display='none';
										
					YAHOO.util.Event.addListener(container.el,"change",function() {
	
						if (container.bodyEl.getElementsByClassName("listField")[0].getWidth() > container.el.getWidth()) {
							container.el.style.width = container.bodyEl.getElementsByClassName("listField")[0].getWidth() + 20 + 'px';
							container.positionTerminals();
						}
							value = container.getValue();
							value.input.inputParams.value = YAHOO.lang.JSON.stringify(value.list);
							container.setValue(value);
							container.redrawAllWires();
							container.bodyEl.getElementsByClassName("inputEx-StringField-wrapper")[0].style.display='none';
							container.initState();
					},this,true);

					YAHOO.util.Event.addListener(container.bodyEl,"click",function() {

						if (container.bodyEl.getElementsByClassName("listField")[0].getWidth() > container.el.getWidth()) {
							container.el.style.width = container.bodyEl.getElementsByClassName("listField")[0].getWidth() + 20 + 'px';
							container.positionTerminals();
						}
							value = container.getValue();
							value.input.inputParams.value = YAHOO.lang.JSON.stringify(value.list);
							container.setValue(value);
							container.redrawAllWires();
							container.bodyEl.getElementsByClassName("inputEx-StringField-wrapper")[0].style.display='none';
							container.initState();
					},this,true);
		
					
					var inputTerminal = container.addTerminal({
						xtype: "WireIt.util.WebUITerminalOutput",
						"name": options.name,
						"ddConfig": {
							"type": options.name.split(":")[1]
						},
						"direction": [0, 1],
						"offsetPosition": {
							"left": Math.floor((container.el.getWidth())/2)-15,
							"bottom": -15
						}
					});
					container.setName(name);

					var wire = {
						"src": {
							"moduleId": WireIt.indexOf(container, this.container.layer.containers),
							"terminal": inputTerminal.options.name
						},
						"tgt": {
							"moduleId": WireIt.indexOf(this.container, this.container.layer.containers),
							"terminal": this.options.name
						}
					};
					this.container.layer.addWire(wire);
					this.redrawAllWires();
				}
			
			},this,true);
		}
//plain types	
		else {
			YAHOO.util.Event.addListener(this.el, "click", function(){
				var pos = YAHOO.util.Dom.getXY(this.el);
				var X = pos[0];
				var Y = pos[1];
				//alert("default: "+ portJSON.default);
				if(typeof(portJSON.default) != 'undefined'){

					var containerConfig = '{"xtype":"WireIt.WebUIInputContainer","title":"input","fields":[{"type": "myType", "inputParams": {"label": "Value", "name": "input", "wirable": false, "value": {"type": "' + type + '", "inputParams": {"name":"' + name + '","value":"'+portJSON.default+'"}}}}],"position":[' + (X - 290) + ',' + (Y - 180) + ']}';

				} else{				
					var containerConfig = '{"xtype":"WireIt.WebUIInputContainer","title":"input","fields":[{"type": "myType", "inputParams": {"label": "Value", "name": "input", "wirable": false, "value": {"type": "' + 	type + '", "inputParams": {"name":"' + name + '"}}}}],"position":[' + (X - 290) + ',' + (Y - 180) + ']}';
				}
				
				if (this.wires.length < 1) {
					var container = this.container.layer.addContainer(YAHOO.lang.JSON.parse(containerConfig));					
					container.schema = portJSON;
					var termType = options.name.split(":")[1];
					var color = "rgb(173, 216, 230)";				
					
					if (termType=="string"){
						color = "rgb(239,224,95)";
					}
					else if (termType=="file"){
						color = "rgb(227,117,80)";
					}
					else if (termType=="boolean"){
						color = "rgb(107,95,59)";
					}
					else if (termType=="number"){
						color = "rgb(31,172,225)";
					}
					else if (termType=="integer"){
						color = "rgb(201,205,204)";
					}
					
					
					var inputTerminal = container.addTerminal({
						xtype: "WireIt.util.WebUITerminalOutput",
						"name": options.name,
						"ddConfig": {
							"type": termType
						},
						"direction": [0, 1],
						"offsetPosition": {
							"left": 86,
							"bottom": -15
						},
					});
					container.setName(options.name.split(":")[0]);
					if (options.name.split(":")[1] == "file") {
						container.renderUploadLink();
					}
					var wire = {
						"src": {
							"moduleId": WireIt.indexOf(container, this.container.layer.containers),
							"terminal": inputTerminal.options.name
						},
						"tgt": {
							"moduleId": WireIt.indexOf(this.container, this.container.layer.containers),
							"terminal": this.options.name
						},
						"color": color
					};
					this.container.layer.addWire(wire);
					this.redrawAllWires();
				}
				
			}, this, true);
		}
	}
};

YAHOO.lang.extend(WireIt.util.WebUITerminalInput, WireIt.WebUITerminal, {
   
   /**
    * Override setOptions to add the default options for WebUITerminalInput
    * @method setOptions
    */
   setOptions: function(options) {
      
    WireIt.util.WebUITerminalInput.superclass.setOptions.call(this,options);
	
	this.options.direction = options.direction || [0,-1];
    	this.options.fakeDirection = options.fakeDirection || [0,1];
	
	this.options.nMaxWires = Infinity;
		
      var type = options.name.split(':')[1];
	   if (type){
	this.options.ddConfig = {
         type: "input:"+type,
         allowedTypes: ["output:"+type,"output:script"]
      	};	
	}
   }      
});
